<?php

declare(strict_types=1);

namespace app\seller\model;

use app\shop\model\Shop;
use sheep\model\Common;
use think\model\concern\SoftDelete;

class Nurse extends Common
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $name = 'nurse';

    // 状态获取器
    public function getStatusAttr($value, $data): string
    {
        $status = [0 => '启用', 1 => '禁用'];
        return $status[$data['status']];
    }

    // 获取服务团队列表
    public function list($params)
    {
        $newWhere = [];
        $search = objectToArray($params['search']);
        if (!empty($params['organ_id'])) {
            $newWhere[] = [$this->name.'.shop_id', '=', $params['organ_id']];
        }
        // 服务团队名称
        if (!empty($search) && isset($search['nurse_name'])) {
            $newWhere[] = ['nurse_name', $search['nurse_name'][1], '%'.$search['nurse_name'][0].'%'];
        }
        // 服务团队编号
        if (!empty($search) && isset($search['nurse_number'])) {
            $newWhere[] = ['nurse_number', $search['nurse_number'][1], $search['nurse_number'][0]];
        }
        // 服务团队职位
        if (!empty($search) && isset($search['nurse_job'])) {
            $newWhere[] = ['nurse_job', $search['nurse_job'][1], '%'.$search['nurse_job'][0].'%'];
        }
        // 服务团队类型查询
        if (!empty($search) && isset($search['nurse_type_id'])) {
            $newWhere[] = ['nurse_type_id', $search['nurse_type_id'][1], $search['nurse_type_id'][0]];
        }
        // 服务团队从业年限
        if (!empty($search) && isset($search['nurse_life'])) {
            $where       = array_values($search['nurse_life']);
            $life        = explode('-', $where[0]);
            $life_string = implode(',', $life);
            $newWhere[]  = ['nurse_life', $where[1], $life_string];
        }
        // 团队类型切换
        if (!empty($params['id'])) {
            $newWhere[] = ['nurse_type_id', '=', $params['id']];
        }
        return self::where([$this->name.'.status' => 0])
            ->withJoin(['nurseType' => ['id', 'nurse_type_name']])
            ->where($newWhere)
            ->order($params['sort'], $params['order'])
            ->paginate([
            'list_rows'=> $params['list_rows'],
            'page' => $params['page'],
        ]);
//        ->fetchSql()->select();
    }

    // 服务团队添加
    public function dataCreate($params): bool
    {
        return $this->allowField(
            [
                'nurse_number', // 编号
                'nurse_name', // 名称
                'nurse_picture', // 头像
                'nurse_job', // 宠物职业
                'nurse_life', // 从业时间
                'nurse_type_id', // 宠护员类型
                'brief_introduction', // 简介
                'proficient_content', // 擅长内容
                'shop_id'
            ])->save($params);
    }

    // 更新服务团队方法
    public function editCreate($params): bool
    {
        $brandModel = self::find($params['id']);
        return $brandModel->allowField([
            'nurse_number', // 编号
            'nurse_name', // 名称
            'nurse_picture', // 头像
            'nurse_job', // 宠物职业
            'nurse_life', // 从业时间
            'nurse_type_id', // 宠护员类型
            'brief_introduction', // 简介
            'proficient_content', // 擅长内容
            'shop_id' // 擅长内容
        ])->save($params);
    }

    // 服务团队详情方法
    public function nurseInfo($params)
    {
        return self::where(['id' => $params])->find();
    }

    // 服务团队删除方法
    public function delData($params): bool
    {
        return self::destroy($params);
    }

    // 模型关联服务团队类型
    public function nurseType()
    {
        return $this->hasOne(NurseType::class, 'id', 'nurse_type_id');
    }
}
